import { Sequelize } from 'sequelize'
import config from '../config/database.js'

const env = process.env.NODE_ENV || 'development'
const dbConfig = config[env]

const sequelize = new Sequelize(
  dbConfig.database,
  dbConfig.username,
  dbConfig.password,
  {
    host: dbConfig.host,
    dialect: dbConfig.dialect
  }
)

const models = {
  User: require('./user')(sequelize),
  Region: require('./region')(sequelize),
  Organization: require('./organization')(sequelize)
}

// 设置模型关联关系
Object.keys(models).forEach(modelName => {
  if (models[modelName].associate) {
    models[modelName].associate(models)
  }
})

export { sequelize }
export default models 